package com.example.demo.service;

import com.example.demo.controller.FriendController;
import com.example.demo.mapper.FriendMapper;
import com.example.demo.model.Friend;
import com.example.demo.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private static Logger log = LoggerFactory.getLogger(FriendController.class);

    public Object getFriendList(HttpServletRequest request) {
        //1.从会话中获取到userId
        HttpSession session = request.getSession(false);
        if(session == null)
        {
            //当前用户会话不存在,用户未登录
            log.warn("用户未登录,会话不存在");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;

    }
}
